home *** CD-ROM | disk | FTP | other *** search
/ 500 MB Nyheder Direkte fra Internet 2 / 500 MB nyheder direkte fra internet CD 2.iso / start / data / text / fortran.txt < prev    next >
Text File  |  1994-09-21  |  77KB  |  1,985 lines

  1.  
  2.                  FUN
  3.   The most popular computer language is BASIC, which I explained 
  4. earlier. Now you'll learn a different computer language, called 
  5. FORTRAN.
  6.   Most maxicomputers and minicomputers understand both BASIC and 
  7. FORTRAN. Some ideas are easier to express in BASIC; others are 
  8. easier in FORTRAN.
  9.   Most scientists and engineers on large computers use FORTRAN, 
  10. not BASIC.
  11.   IBM invented the first version of FORTRAN in 1957. Then came 
  12. improvements, called FORTRAN II, FORTRAN III, and FORTRAN IV. The 
  13. next version was called FORTRAN 77 because it was invented in 
  14. 1977. The newest version is called FORTRAN 90 because it was 
  15. invented in 1990.
  16.   Some computers use FORTRAN 77 or FORTRAN 90. Others still use 
  17. FORTRAN IV or a slightly souped-up version of it (called FORTRAN 
  18. IV-EXTENDED or FORTRAN V or WATFOR or WATFIV or FORTRAN 10).
  19.   This chapter explains the popular FORTRAN features that work on 
  20. practically all computers.
  21.  
  22.            Simple programs
  23.   Here's a FORTRAN program:
  24.       PRINT 10
  25. 10    FORMAT (1X,'CHIPMUNKS ARE CHUBBY')
  26.       PRINT 20
  27. 20    FORMAT (1X,'GOLDFISH GIGGLE')
  28.       PRINT 10
  29.       END
  30.   The top line says to print what's in line 10, so the computer 
  31. will print CHIPMUNKS ARE CHUBBY. The next line says to print 
  32. what's in line 20, so the computer will print GOLDFISH GIGGLE. 
  33. The next line says to print what's in line 10, so the computer 
  34. will print CHIPMUNKS ARE CHUBBY again.Altogether, the program 
  35. makes the computer print:
  36. CHIPMUNKS ARE CHUBBY
  37. GOLDFISH GIGGLE
  38. CHIPMUNKS ARE CHUBBY
  39.   Notice:
  40. Each program line is indented 6 spaces, so it begins in the 7th 
  41. position.
  42. Each FORMAT begins with 1X.
  43. Each string is enclosed in apostrophes.
  44.   You must number every line that's referred to. For example, you 
  45. must number the FORMAT lines, since the PRINT lines refer to 
  46. them. You don't have to number the PRINT lines.
  47.   The bottom line of every FORTRAN program must be END.
  48.   Different versions On PDP computers, say TYPE instead of PRINT. 
  49. On CDC computers using TS FORTRAN, replace each apostrophe by an 
  50. asterisk. On IBM computers, put STOP above END, so the bottom two 
  51. lines of your program are:
  52.       STOP
  53.       END
  54.  
  55.                                          How to type the program 
  56. Ask the people in your computer center how to feed a FORTRAN 
  57. program into the computer. On some computers, you must put an 
  58. edit number in front of each line:
  59. 00100         PRINT 10
  60. 00110   10    FORMAT (1X,'CHIPMUNKS ARE CHUBBY')
  61. 00120         PRINT 20
  62. 00130   20    FORMAT (1X,'GOLDFISH GIGGLE')
  63. 00140         PRINT 10
  64. 00150         END
  65.                                          Some computers don't 
  66. require you to indent each line. To indent quickly on PDP 
  67. computers, hold down the CONTROL key, while you type the letter 
  68. I.
  69.                                          Be brief Each line of 
  70. your program must be brief: no more than 72 characters, including 
  71. the spaces at the beginning of the line.
  72.  
  73.                                                  Carriage controls
  74.                                          The 1X at the beginning 
  75. of each FORMAT is called the carriage control. It means: print 
  76. the format normally.
  77.                                          For weirder printing, 
  78. replace the 1X by '0' or '1' or '+':
  79.       PRINT 10
  80. 10    FORMAT (1X,'NIFTY')
  81.       PRINT 20
  82. 20    FORMAT ('0','SAL')
  83.       END
  84.                                          In line 10, the 1X makes 
  85. the computer print NIFTY normally. In line 20, the 
  86. zero-in-apostrophes make the computer leave a blank line, then 
  87. print SAL.
  88.                                          The computer will print:
  89. NIFTY
  90.  
  91. SAL
  92.                                          Suppose you change the 
  93. carriage control to '1':
  94.       PRINT 10
  95. 10    FORMAT (1X,'NIFTY')
  96.       PRINT 20
  97. 20    FORMAT ('1','SAL')
  98.       END
  99. If your terminal uses paper, the '1' makes the computer print SAL 
  100. on a new page. If your terminal uses a screen instead of paper, 
  101. the '1' makes the computer erase the screen before printing SAL.
  102.                                          Suppose you change the 
  103. carriage control to '+':
  104.       PRINT 10
  105. 10    FORMAT (1X,'NIFTY')
  106.       PRINT 20
  107. 20    FORMAT ('+','SAL')
  108.       END
  109. If your terminal uses paper, the '+' makes the computer print SAL 
  110. in the same place as NIFTY, like this:
  111. NIFTYSAL
  112. If your terminal uses a screen instead, the computer will print 
  113. NIFTY, but then the NIF will suddenly disappear, and you'll see 
  114. SALTY.
  115.   To print the symbol 0-, print 0 in the same place as -. To 
  116. print the symbol =/, print = in the same place as /. To print 
  117. 0=-/A, print 0=A in the same place as -/. If your terminal uses 
  118. paper, the program prints 0=-/A:
  119.       PRINT 10
  120. 10    FORMAT (1X,'0=A')
  121.       PRINT 20
  122. 20    FORMAT ('+','-/')
  123.       END
  124.   Different versions If you're using a Hazeltine terminal or CDC 
  125. TS FORTRAN, the carriage controls won't work.
  126.  
  127.       Fancy formats
  128.   In the format line, you can play fancy tricks.
  129.   Multiple fields Examine this program:
  130.       PRINT 10
  131. 10    FORMAT (1X,'JOHN','NY')
  132.       END
  133. The FORMAT consists of three fields: the first is the carriage 
  134. control 1X, the second is `JOHN', and the third is `NY'. The 
  135. computer will print JOHN and NY on the same line:
  136. JOHNNY
  137.   Another example:
  138.       PRINT
  139. 10    FORMAT (1X,'EAT A',8X,'MEATBALL')
  140.       END
  141. The computer will print EAT A, then 8 blank spaces, then 
  142. MEATBALL:
  143. EAT A        MEATBALL
  144.   This program does the same thing:
  145.       PRINT 10
  146. 10    FORMAT (1X,'EAT A',T15,'MEATBALL')
  147.       END
  148. It makes the computer print EAT A, then Tab over to the 15th 
  149. position on the line, and print MEATBALL. When the computer tabs 
  150. to the 15th position, it considers the carriage control to be the 
  151. first ``position''; the E in EAT is the first character the 
  152. computer will print, and the computer considers it to be the 
  153. second ``position''; the A in EAT is the second character the 
  154. computer will print, and the computer considers it to be the 
  155. third ``position''; the M in MEATBALL is the 14th character the 
  156. computer will print, since the T15 says it is the 15th 
  157. ``position''.
  158.   Here's another program for meatball lovers:
  159.       PRINT 10
  160. 10    FORMAT (1X,'EAT A',1X,'MEATBALL')
  161. The computer will print EAT A, then 1 blank space, then MEATBALL:
  162. EAT A MEATBALL
  163. If you say X instead of 1X, the computer will gripe.
  164.                              Multiple records This program quotes 
  165. Julius Caesar:
  166.       PRINT 10
  167. 10    FORMAT (1X,'I CAME')
  168.       PRINT 20
  169. 20    FORMAT (1X,'I SAW')
  170.       PRINT 30
  171. 30    FORMAT (1X,'I CONQUERED')
  172.       END
  173. The computer will print:
  174. I CAME
  175. I SAW
  176. I CONQUERED
  177.                              This program does the same thing:
  178.       PRINT 10
  179. 10    FORMAT (1X,'I CAME'/1X,'I SAW'/1X,'I CONQUERED')
  180.       END
  181. Line 10 consists of 3 records: the first is 1X,`I CAME'; the 
  182. second is 1X,`I SAW'; the third is 1X,`I CONQUERED'. Each record 
  183. begins with a carriage control; the records are separated by 
  184. slashes. The computer will print each record on a separate line.
  185.                              Example:
  186.       PRINT 10
  187. 10    FORMAT (1X,'PLEASE'/1X,'NIBBLE'//1X,'MY'/////1X,'CHEESE')
  188.       END
  189. Line 10 makes the computer print several lines. The first line 
  190. will say PLEASE. The next line will say NIBBLE. The next line 
  191. will be blank. The next line will say MY. The next 4 lines will 
  192. be blank. The last line will say CHEESE. So altogether, the 
  193. computer will print:
  194. PLEASE
  195. NIBBLE
  196.  
  197. MY
  198.  
  199.  
  200.  
  201.  
  202. CHEESE
  203.                              Repeated formats The computer can 
  204. feel very depressed:
  205.       PRINT 10
  206. 10    FORMAT (1X,'I FEEL ',3('DOWN'))
  207.       END
  208. Line 10 is an abbreviation for this format:
  209.               1X,'I FEEL ','DOWN','DOWN','DOWN'
  210. The computer will print:
  211. I FEEL DOWNDOWNDOWN
  212.                              Let's burp and pray:
  213.       PRINT 10
  214. 10    FORMAT (1X,'JACK ',2('BURPS'/1X,'MARY '),'ALSO PRAYS')
  215.       END
  216. Line 10 is an abbreviation for this format:
  217.               1X,'JACK ','BURPS'/1X,'MARY ','BURPS'/1X,'MARY 
  218. ','ALSO PRAYS'
  219. The computer will print:
  220. JACK BURPS
  221. MARY BURPS
  222. MARY ALSO PRAYS
  223.                              Double apostrophe To make the 
  224. computer print an apostrophe, use two apostrophes next to each 
  225. other.
  226.       PRINT 10
  227. 10    FORMAT (1X,'MOMMY ISN''T HERE')
  228.       END
  229. The computer will print:
  230. MOMMY ISN'T HERE
  231.  
  232.                   Continuation
  233.   Some computers look at only the first 72 characters of each 
  234. line of your program: if your line contains more than 72 
  235. characters, it won't work.
  236.   If you want to type a long statement, type just the first 72 
  237. characters. On the line below, type the remaining characters, 
  238. beginning in the 7th print position; and in the 6th print 
  239. position type a 6:
  240.       PRINT 10 
  241. 10    FORMAT (1X,'I LIKE ROSES IN MY TEA.'/1X,'THEY MAKE IT GLOW 
  242. RED, LI
  243.      6KE HOT BLOOD.')
  244.       END
  245. The computer will print:
  246. I LIKE ROSES IN MY TEA.
  247. THEY MAKE IT GLOW RED, LIKE HOT BLOOD.
  248. A line that has a 6 in column 6 is called a continuation line, 
  249. because it's a continuation of the line above it.
  250.   (I suggest you put a 6 in column 6, or a * in column 6, or a $ 
  251. in column 6. In fact, you can put any character in column 6, 
  252. except a zero. Choose your favorite character; the computer 
  253. doesn't care.)
  254.   Different versions On PDP computers, put the 6 immediately 
  255. after a controlled I, instead of in column 6:
  256. 10      FORMAT (1X,'I LIKE ROSES IN MY TEA.'/1X,'THEY MAKE IT 
  257. GLOW RED, LI
  258.         6KE HOT BLOOD.')
  259. (I suggest you put a 6 after the controlled I. But you can put in 
  260. any non-zero digit, instead of a 6.)
  261.   On CDC computers using TS FORTRAN, type a + instead of a 6, and 
  262. put it immediately after the edit number, with no intervening 
  263. spaces:
  264. 00120 10 FORMAT (1X,'I LIKE ROSES IN MY TEA.'/1X,'THEY MAKE IT 
  265. GLOW RED, LI
  266. 00130+KE HOT BLOOD.')
  267.  
  268.                       GO TO
  269.   You can say GO TO:
  270. 10    PRINT 20
  271. 20    FORMAT (1X,'CAT')
  272.       PRINT 30
  273. 30    FORMAT (1X,'DOG')
  274.       GO TO 10
  275.       END
  276. The top line says to print what's in line 20, so the computer 
  277. will print:
  278. CAT
  279. The next line says to print what's in line 30, so the computer 
  280. will print:
  281. DOG
  282. The next line makes the computer go back to line 10. The computer 
  283. will print CAT again, then DOG again, then jump back to line 10 
  284. again. . . . The computer will try to print the words CAT and DOG 
  285. again and again, forever.
  286.  
  287.                       STOP
  288.   The computer understands the word STOP:
  289.       PRINT 10
  290. 10    FORMAT (1X,'BUBBLE GUM')
  291.       PRINT 20
  292. 20    FORMAT (1X,'SHAKESPEARE')
  293.       STOP
  294.       PRINT 30
  295. 30    FORMAT (1X,'DADA')
  296.       END
  297. The top line says to print what's in line 10, so the computer 
  298. will print BUBBLE GUM. The next line says to print what's in line 
  299. 20, so the computer will print SHAKESPEARE. The next line says 
  300. STOP, so the computer will stop. It will never print DADA.
  301.  
  302.  
  303.                       MATH
  304.   FORTRAN handles math rather well.
  305.  
  306.           Integers versus real numbers
  307.   FORTRAN distinguishes between integers and real numbers. Here's 
  308. how FORTRAN defines them. . . . 
  309.   An integer contains no decimal point and no exponent.
  310. IntegerNot an integerComment
  311. -27   -27.0     An integer contains no decimal point.
  312. 50000 5E4       An integer contains no exponents.
  313. A real number contains either a decimal point or the letter E.
  314. Real numberNot a real number
  315. -27.0     -27
  316. 2.35E8    235000000
  317. 5E4       50000
  318.   The largest permissible integer is different from the largest 
  319. permissible real:
  320. Computer          Largest integerLargest realTiniest real
  321. PDP-11 using FORTRAN IV          327671.7E382.9E-39
  322. PDP-11 using FORTRAN IV-PLUS     21474836471.7E382.9E-39
  323. PDP-10 or Honeywell    343597383671.7E382.9E-39
  324. IBM mainframe          21474836477.2E75 5.4E-79
  325. CDC               2814749767106552.5E3223.1E-294
  326.   Integers are also called fixed-point numbers. Real numbers are 
  327. called floating-point numbers.
  328.  
  329.                     Variables
  330.   In BASIC, X can stand for a number, such as 3.7. The same is 
  331. true in FORTRAN. A variable can be a letter (such as X) or a 
  332. letter-followed-by-a-combination-of-letters-and-digits (such as 
  333. FUN4U2).
  334.   The variable must be short: no more than 6 characters. AVERAGE 
  335. is too long: say AVERAG instead.
  336.   If the variable begins with I, J, K, L, M, or N, it stands for 
  337. an integer. If it begins with some other letter, it stands for a 
  338. real number.
  339.  
  340.              Using integer variables
  341.   Here's a simple example:
  342.       JUNKY=-47
  343.       PRINT 10, JUNKY
  344. 10    FORMAT (1X,I3)
  345.       END
  346.   Since JUNKY begins with J, it stands for an integer. The first 
  347. line says JUNKY stands for the integer -47. The second line says 
  348. to print JUNKY, using line 10. Line 10 explains how to print 
  349. JUNKY. The I3 means: print it as an Integer having 3 characters. 
  350. The computer will print:
  351. -47
  352.   If you change the I3 to I4, the computer will print JUNKY as an 
  353. Integer having 4 characters. To print a total of 4 characters, 
  354. the computer will print a blank space in front of -47, like this:
  355.  -47
  356.   If you change to I5, the computer will print JUNKY as an 
  357. Integer having 5 characters, by printing two blank spaces in 
  358. front of -47:
  359.   -47
  360.   If you change to I2, the computer will try to print JUNKY as an 
  361. integer having 2 characters. But it's impossible to express -47 
  362. by using only 2 characters. The computer will obey the format and 
  363. print 2 characters, but will make them asterisks:
  364. **
  365.  
  366.                                                      Another 
  367. example:
  368.       NUM=31.9
  369.       PRINT 10, NUM
  370. 10    FORMAT (1X,I4)
  371.       END
  372. Since NUM begins with N, it stands for an integer. The program's 
  373. top line tries to make NUM stand for 31.9; but that's impossible, 
  374. since 31.9 isn't an integer. The computer will omit the .9 and 
  375. make NUM stand for the integer 31. The computer will print 31, 
  376. using an I4 format:
  377.   31
  378.                                                      Example:
  379.       JOE=-5.8
  380.       PRINT 10, JOE
  381. 10    FORMAT (1X,I4)
  382.       END
  383. The computer will set JOE equal to the integer -5 and print it:
  384.   -5
  385.  
  386.   Example:
  387.       JAIL=74
  388.       KRIMNL=829
  389.       PRINT 10, JAIL,KRIMNL
  390. 10    FORMAT (1X,I2,I3)
  391.       END
  392. Since JAIL begins with J, and KRIMNL begins with K, they're both 
  393. integers. The computer will print JAIL and KRIMNL, using the 
  394. format in line 10. The format says to print a 2-character 
  395. integer, then a 3-character integer. The computer will print:
  396. 74829
  397. If you change the format to (1X,I2,4X,I3), the computer will 
  398. print a 2-character integer, then 4 blanks, then a 3-character 
  399. integer:
  400. 74    829
  401. If you change the format to ___ 
  402. 10    FORMAT (1X,'JAIL NUMBER',1X,I2,1X,'CONTAINS 
  403. CRIMINAL',1X,I3)
  404. the computer will print JAIL NUMBER, then a blank, then a 
  405. 2-character integer, then a blank, then CONTAINS CRIMINAL, then a 
  406. blank, then a 3-character integer:
  407. JAIL NUMBER 74 CONTAINS CRIMINAL 829
  408.   Example:
  409.       J=43
  410.       K=75
  411.       L=96
  412.       M=81
  413.       N=24
  414.       PRINT 10, J,K,L,M,N
  415. 10    FORMAT (1X,I2,I2,I2,I2,I2)
  416.       END
  417. The computer will print 43, then 75, then 96, then 81, then 24:
  418. 4375968124
  419. You can write that format more briefly:
  420. 10    FORMAT (1X,5I2)
  421. If you change the format to (1X,5I3), the computer will print 
  422. each integer as 3 characters ___ a blank followed by two digits:
  423.  43 75 96 81 24
  424. If you change the format to (1X,I3), the computer will print only 
  425. one integer per line:
  426.  43
  427.  75
  428.  96
  429.  81
  430.  24
  431. If you change the format to (1X,2I3), the computer will print 2 
  432. integers per line:
  433.  43 75
  434.  96 81
  435.  24
  436. If you change the format to (1X,'GOSH',I3,1X,'SUPERB',I3,1X,'JEEP
  437. ERS'), the computer will print 2 integers per line:
  438. GOSH 43 SUPERB 75 JEEPERS
  439. GOSH 96 SUPERB 81 JEEPERS
  440. GOSH 24 SUPERB
  441.   To be safe, use I14 format for integers. On most computers, I14 
  442. handles even the largest integers, and prints blank spaces 
  443. between them.
  444.  
  445.               Using real variables
  446.   The I format is only for integers. For real numbers, use F or G 
  447. format instead.
  448.   F format The F format is easy to understand:
  449.       RADIUS=-586.39
  450.       PRINT 10, RADIUS
  451. 10    FORMAT (1X,F7.2)
  452.       END
  453. Since RADIUS doesn't begin with I, J, K, L, M, or N, it stands 
  454. for a real number. The first line says RADIUS stands for the real 
  455. number -586.39. The second line says to print RADIUS, using the 
  456. format in line 10. The F7.2 means: print it as a floating-point 
  457. number having 7 characters, 2 of them after the decimal point. 
  458. The computer will print:
  459. -586.39
  460.   If you change the F7.2 to a different format, the following 
  461. chart shows what happens; in the chart, each ■ represents a blank 
  462. space:
  463. FormatWhat the computer printsComment
  464. F8.2  ■-586.39          To print 8 characters instead of 7,
  465.                         it prints a blank space at the beginning.
  466.  
  467. F8.3  -586.390          To print 3 characters after the decimal 
  468. point
  469.                         instead of 2, it prints a zero at the 
  470. end.
  471.  
  472. F8.1  ■■-586.4          To print 1 character after the decimal 
  473. point
  474.                         instead of 2, it rounds the .39 to 4.
  475.  
  476. F8.4  ********          To print 4 characters after the decimal 
  477. point,
  478.                         the computer would have to print 
  479. -586.3900.
  480.                         Since that requires more than 8 
  481. characters,
  482.                         the computer complains by printing 
  483. asterisks.
  484.   G format To print a real number, the safest format is G14.6, 
  485. because G14.6 can handle any real number well, even if the number 
  486. is very large or very tiny.
  487.   G14.6 prints 14 characters altogether, 6 of which are 
  488. significant digits. Here are examples of numbers printed in G14.6 
  489. format:
  490. ■-0.283941E-29
  491. ■■0.293027■■■■
  492. ■■■5.34523■■■■
  493. ■■■39.4539■■■■
  494. ■■■47802.3■■■■
  495. ■■■986327.■■■■
  496. ■■0.288341E+24
  497.   Example:
  498.       PRUNES=17
  499.       PRINT 10, PRUNES
  500. 10    FORMAT (1X,G14.6)
  501.       END
  502. Since PRUNES doesn't begin with I, J, K, L, M, or N, it stands 
  503. for a real number. When the computer encounters the first line of 
  504. the program, it will set PRUNES equal to the real number 17.0. It 
  505. will print:
  506.    17.0000
  507. The program will run faster if you change the top line to this:
  508.       PRUNES=17.0
  509.   E format For real numbers, the usual formats are F and G, but 
  510. another option is E.
  511.   If you say E14.6 instead of G14.6, the computer will print an E 
  512. in the answer. Here are examples:
  513. Using G14.6 formatUsing E14.6 format
  514. ■-0.283941E-29■-0.283941E-29
  515. ■■0.293027■■■■■■0.293027E+00
  516. ■■■5.34523■■■■■■0.534523E+01
  517. ■■■39.4539■■■■■■0.394539E+02
  518. ■■■47802.3■■■■■■0.478023E+05
  519. ■■■986327.■■■■■■0.986327E+06
  520. ■■0.288341E+24■■0.288341E+24
  521.   The G14.6 format is easier for a human to read than E14.6. But 
  522. most programmers are stupid, don't know about G14.6, and use 
  523. E14.6 instead.
  524.                                                      P format 
  525. FORTRAN's notation differs from BASIC.
  526.                                                      If you ask 
  527. the computer to print 288341000000000000000000.0 in FORTRAN by 
  528. using G14.6 (or E14.6), the computer will normally print a 0 
  529. before the decimal point, like this: 0.288341E+24. In BASIC, the 
  530. computer will print a non-zero digit before the decimal point, 
  531. like this: 2.88341E+23.
  532.                                                      If you're 
  533. writing a program in FORTRAN, but you prefer BASIC's notation, 
  534. ask for 1PG14.6 (or 1PE14.6). The 1P makes the computer imitate 
  535. BASIC. But if a FORMAT contains 1PG, it must not contain F 
  536. afterwards; this will print a wrong answer:
  537.       FORMAT (1X,1PG14.6,F8.2)
  538.                         
  539.                    Here is P.   The F afterwards
  540.                                    prints a wrong answer!
  541.  
  542.                                                           Operations
  543.                                                      For 
  544. addition, subtraction, multiplication, and division, FORTRAN uses 
  545. the same symbols as BASIC.
  546.       N=2*(3+1)
  547.       S=7.3+2.1
  548.       PRINT 10, N,S
  549. 10    FORMAT (1X,I14,G14.6)
  550.       END
  551. Since N is 8, and S is 9.4, the computer will print:
  552.              8   9.40000
  553.                                                      Exponents 
  554. For exponents, FORTRAN uses a double star:
  555.       J=7**2
  556.       P=.5**3
  557.       PRINT 10, J,P
  558. 10    FORMAT (1X,I14,G14.6)
  559.       END
  560. Since J is 72 (which is 49), and P is .53 (which is .125), the 
  561. computer will print:
  562.             49   0.125000
  563.                                                      For negative 
  564. exponents, you need parentheses. You must say 6.1**(-2), not 
  565. 6.1**-2.
  566.                                                      What type of 
  567. answer? When you combine integers, the answer's an integer:
  568. 2+3 is 5
  569. 8-8 is 0
  570. 2*4 is 8
  571. 399/100  is 3 (not 3.99)
  572. 11/4     is 2 (not 2.75)
  573. 3/4      is 0 (not 0.75)
  574. 10**(-2) is 0 (not 0.01)
  575.                                                      When you 
  576. combine real numbers, the answer is real:
  577. 4.1+2.9      is 7.0 (not 7)
  578. 8.0-8.0      is 0.0 (not 0)
  579. 399.0/100.0  is 3.99
  580. 11.0/4.0     is 2.75
  581. 3.0/4.0      is .75
  582. 10.0**(-2.0) is .01
  583.  
  584.   When you combine an integer with a real number, the answer is 
  585. real:
  586. 3+2.0      is 5.0
  587. 399/100.0  is 3.99
  588. 11/4.0     is 2.75
  589. 3/4.0      is .75
  590. 10.0**(-2) is .01
  591.   Compare these:
  592. 7/10*10    is 0(because 7/10 is 0)
  593. 7/10*10.0  is 0.0(because 0*10.0 is 0.0)
  594. 7/10.0*10  is 7.0(because 7/10.0 is .7)
  595.   Example:
  596.       JERK=20.0+30.9
  597.       PRINT 10, JERK
  598. 10    FORMAT (1X,I14)
  599.       END
  600. Since JERK begins with J, it stands for an integer. Since 
  601. 20.9+30.9 is 51.8, JERK stands for the integer 51. The computer 
  602. will print:
  603.             51
  604.   Another example:
  605.       APPLE=37/10
  606.       PRINT 10, APPLE
  607. 10    FORMAT (1X,G14.6)
  608.       END
  609. Since APPLE begins with A, it stands for a real number. Since 
  610. 37/10 is 3, APPLE stands for the real number 3.0. The computer 
  611. will print:
  612.    3.00000
  613.   Crimes that slow down the computer cop If you commit one of 
  614. these crimes, the computer will work slowly. . . . 
  615. little crime: use a real number
  616. medium crime: mix reals with integers
  617. big crime: use a real exponent
  618.   For example, the computer handles 2.0+2.0 slower than 2+2, 
  619. because 2.0+2.0 is a little crime.
  620.   The bigger the crime, the slower the computer works. For 
  621. example, the computer handles 2.1+7 (which is a medium crime) 
  622. slower than 2.1+7.0 (which is just a little crime). Likewise, X=0 
  623. (a medium crime) gets handled slower than X=0.0 (a little crime).
  624.   5.1**2.0 is a big crime, since its exponent (2.0) is real. The 
  625. computer handles it slower than 5.1**2, which is just a medium 
  626. crime.
  627.   5**3.1 is a gigantic crime, since it's a medium crime and a big 
  628. crime simultaneously. Because the crime's so gigantic, some 
  629. computers refuse to handle it. Say 5.0**3.1 instead.
  630.                                         Advice about variables
  631.                              FORTRAN, like BASIC, distinguishes 
  632. variables, constants, and expressions:
  633. X                                is a variable
  634. 2.7                              is not a variable; it's a 
  635. numeric constant
  636. `LOVE'                           is not a variable; it's a string 
  637. constant
  638. X+Y                              is not a variable; it's an 
  639. expression
  640.                              In a PRINT statement, some computers 
  641. allow only variables. . . . 
  642.                              allowed: PRINT 10, X
  643. not                          allowed: PRINT 10, 2.7instead, say 
  644. X=2.7 and PRINT 10, X
  645. not                          allowed: PRINT 10, `LOVE'instead, 
  646. say PRINT 10 and 10 FORMAT (1X,`LOVE')
  647. not                          allowed: PRINT 10, X+Yinstead, say 
  648. Z=X+Y and PRINT 10, Z
  649. Other computers are more generous and allow anything. Find out 
  650. about yours.
  651.                              To help other humans understand your 
  652. program, use long variable names throughout your program. Say 
  653. RADIUS, not R; say AREA, not A; say VOLUME, not V; say SUM, not 
  654. S; say TOTAL, not T. Because FORTRAN's variables are restricted 
  655. to six characters, you might have to omit the last few syllables 
  656. (revolutions becomes REVOLU) or the last few vowels (RVLTNS).
  657.                              If you want a variable to be real, 
  658. but its English name begins with I, J, K, L, M, or N, begin its 
  659. FORTRAN name with an A (mass becomes AMAS; length becomes ALENGT 
  660. or ALNGTH). If you want a variable to be an integer, but its 
  661. English name doesn't begin with I, J, K, L, M, or N, begin its 
  662. FORTRAN name with an I (population becomes IPOPUL) or misspell it 
  663. (count becomes KOUNT) or choose a synonym (instead of position, 
  664. say location, which is LOCATN).
  665.  
  666.             PLEASANT I/O
  667.   You learned how to make the computer PRINT by using a FORMAT. 
  668. Now you'll learn about PRINT's opposite (READ) and how to omit 
  669. FORMATs altogether.
  670.  
  671.                 READ
  672.   The computer can READ.
  673.       PRINT 10
  674. 10    FORMAT (1X,'TYPE SOME DIGITS')
  675.       READ 20, N
  676. 20    FORMAT (I4)
  677.       PRINT 30, N
  678. 30    FORMAT (1X,I4)
  679.       END
  680.   When you run the program, here's what happens. . . . 
  681.   The top two lines make the computer print:
  682. TYPE SOME DIGITS
  683.   The word READ makes the computer wait for you to type 
  684. something; it's like the BASIC word INPUT. The computer will wait 
  685. for you to type the value of N, but line 20's FORMAT makes the 
  686. computer read just the first 4 characters. For example, if you 
  687. type ___ 
  688. -75198622
  689. the computer will read just the first 4 characters, which are 
  690. -751; it will ignore the 98622; so N will be -751. Line 30's 
  691. FORMAT makes the computer print:
  692. -751
  693. Altogether, the run looks like this:
  694. The computer says:TYPE SOME DIGITS
  695. You say:    -75198622
  696. The computer replies:-751
  697.   Hassles Line 30's FORMAT contains a carriage control 1X, but 
  698. line 20's FORMAT omits the carriage control. Put a carriage 
  699. control in formats that PRINT, but not in formats that READ.
  700.   On PDP computers, say ACCEPT instead of READ.
  701.   Blank spaces If you input a blank space, the computer treats it 
  702. as a zero.
  703.   For example, suppose you input:
  704. -3 28219
  705. Because of the I4 format, the computer will read just the first 4 
  706. characters, which are -3 2; the blank space between the 3 and the 
  707. 2 is treated as a zero, so N will be -302.
  708.   Suppose you input:
  709. 57
  710. Because of the I4 format, the computer will read the 5, the 7, 
  711. and two blanks. Since the blanks are treated as zeros, N will be 
  712. 5700.
  713.   Suppose you input:
  714. ■■■9527
  715. Because of the I4 format, the computer will read the three 
  716. beginning blanks and the 9. Since the blanks are treated as 
  717. zeros, N will be 0009, which is 9. Line 30 makes the computer 
  718. print:
  719.    9
  720.  
  721.                                          Multiple 
  722. variablesSuppose you write a program containing these lines:
  723.       READ 20, L,M,N
  724. 20    FORMAT (I3,I4,2X,I2)
  725. When you run that program, suppose you input:
  726. 58194138972824
  727. The I3 format makes the first 3 characters (581) be L. The I4 
  728. format makes the next 4 characters (9413) be M. The 2X format 
  729. makes the next 2 characters (89) be skipped over. The I2 format 
  730. makes the next two characters (72) be N. The remaining characters 
  731. (824) are ignored. So the line is split like this. . . . 
  732. Line you input:                                          
  733. 58194138972824
  734. Fields in the FORMAT statement:                              I4 
  735. 2XI2
  736. Variables in the READ statement:                          L        
  737.                                                          │  │   │ 
  738. │ │
  739.                                                          │I3│   │ 
  740. │ │
  741.                                                          │  │ M │ 
  742. │N│
  743.                                          Suppose you write a 
  744. program containing these lines:
  745.       READ 20, J,K,L,M,N
  746. 20    FORMAT (2I3)
  747. The format says to read two 3-character integers on each line. 
  748. Suppose you input:
  749. 78345692
  750. 85431684
  751. 46185327
  752. J will be 783, and K will be 456. L will be 854, and M will be 
  753. 316. N will be 461.
  754.                                          Real variables Here's 
  755. how to input a real number:
  756.       PRINT 10
  757. 10    FORMAT (1X,'TYPE SOME DIGITS')
  758.       READ 20, P
  759. 20    FORMAT (F6.2)
  760.       PRINT 30, P
  761. 30    FORMAT (1X,G14.6)
  762.       END
  763.                                          The F6.2 format means: 
  764. read 6 characters; if they don't contain the decimal point, 
  765. insert it before the last 2 digits.
  766.                                          For example, suppose you 
  767. input:
  768. 327514968
  769. The computer reads the first 6 characters (327514). Since they 
  770. don't contain the decimal point, the computer inserts it before 
  771. the last 2 digits, so P is 3275.14. Line 30 prints:
  772.    3275.14
  773.                                          Suppose you input:
  774. 7.5423967
  775. The computer reads the first 6 characters (7.5423). Since they 
  776. already contain the decimal point, P is 7.5423. Line 30 says to 
  777. print that number by using 6 significant digits, so the computer 
  778. prints:
  779.    7.54230
  780.                                          Suppose you input:
  781. 497E3
  782. The computer reads 6 characters (497E3, followed by a blank). 
  783. Since blanks are treated as zeros, the computer gets 497E30. 
  784. Since 497E30 doesn't contain the decimal point, the computer 
  785. inserts it before the last 2 digits, so P is 4.97E30. Line 30 
  786. prints:
  787.   0.497000E+31
  788.  
  789.           Omitting formats
  790.   Most computers let you omit numeric formats. This program works 
  791. on most modern computers (such as computers having FORTRAN 77, 
  792. PDP-20 computers, PDP-10 computers using FORTRAN 10, PDP-11 
  793. computers using FORTRAN IV-PLUS, CDC computers using FORTRAN 
  794. IV-EXTENDED, and IBM computers using FORTRAN H-EXTENDED):
  795.       PRINT 10          On PDP computers,
  796. 10    FORMAT (1X,'TYPE TWO INTEGERS')say TYPE instead of
  797.       READ *, M,N       PRINT, and ACCEPT
  798.       ISUM=M+N          instead of READ.
  799.       PRINT *, ISUM
  800.       END
  801.   The word READ is followed by an asterisk, instead of a FORMAT 
  802. number. The last PRINT is followed by an asterisk also. The 
  803. asterisk makes the computer invent its own FORMAT. To make the 
  804. program add 241 and 82976, input the numbers, separated by a 
  805. comma:
  806. 241,82976
  807. The computer will notice the comma's location and automatically 
  808. use an I3 format for 241, a 1X format to skip over the comma, and 
  809. an I5 format for 82976. To print ISUM, the computer will use a 
  810. safe format, such as I14 or I15.
  811.   By omitting formats, you gain two advantages:
  812.   1. You can write FORTRAN programs more quickly.
  813.   2. The person who inputs doesn't have to worry about whether 
  814. his spacing matches the format. The computer invents a format 
  815. that matches his input.
  816.   Different versions On CDC computers using TS FORTRAN and on 
  817. Honeywell computers, omit the asterisk after READ and PRINT:
  818.       PRINT 10
  819. 10    FORMAT (1X,'TYPE THE NUMBERS')
  820.       READ, M,N
  821.       ISUM=M+N
  822.       PRINT, ISUM
  823.       END
  824.   On PDP-10 computers using F40 FORTRAN, and on PDP-11 computers 
  825. using regular FORTRAN IV, you need FORMATs, but omit the number 
  826. in the I format:
  827.       TYPE 10
  828. 10    FORMAT (1X,'TYPE THE NUMBERS')
  829.       ACCEPT 20, M,N
  830. 20    FORMAT (1X,2I)
  831.       ISUM=M+N
  832.       TYPE 30, ISUM
  833. 30    FORMAT (1X,I)
  834.       END
  835.   Real numbers You can use similar short cuts for real numbers.
  836.  
  837.  
  838.                       LOGIC
  839.   You learned how to say GO TO and STOP. Taking those concepts 
  840. further, let's see how to say IF and DO and give a computed GO 
  841. TO.
  842.  
  843.                        IF
  844.   FORTRAN uses these clauses:
  845. Clause    Meaning
  846. IF (I .LT. 5)If I is Less Than 5
  847. IF (I .GT. 5)If I is Greater Than 5
  848. IF (I .LE. 5)If I is Less than or Equal to 5
  849. IF (I .GE. 5)If I is Greater than or Equal to 5
  850. IF (I .EQ. 5)If I is EQual to 5
  851. IF (I .NE. 5)If I is Not Equal to 5
  852.   Notice that each relational operator (such as LT) must be 
  853. enclosed in periods, and each condition (such as I .LT. 5) must 
  854. be enclosed in parentheses.
  855.   By using those clauses, you can build statements:
  856. Statement       Meaning
  857. IF (I .LT. 5) J=3If I is Less Than 5, let J=3
  858. IF (I .LT. 5) GO TO 80If I is Less Than 5, go to line 80.
  859. IF (I .LT. 5) STOPIf I is Less Than 5, stop.
  860. IF (I .LT. 5) PRINT 10, JIf I is Less Than 5, print J using line 
  861. 10's FORMAT.
  862.   You can use the words AND and OR:
  863. Idea            How to say it in FORTRAN
  864. If I is 2 or 9 or 13IF (I .EQ. 2 .OR. I .EQ. 9 .OR. I .EQ. 13)
  865. If I is an integer from 1 to 8IF (I .GE. 1 .AND. I .LE. 8)
  866. If X<Y<Z        IF (X .LT. Y .AND. Y .LT. Z)
  867. If A is less than both B and CIF (A .LT. B .AND. A .LT. C)
  868. If X negative or between 5 & 9IF (X .LT. 0.0 .OR. X .GE. 5.0. 
  869. .AND. X .LE. 9.0)
  870.   Take this test: cover the column that says ``How to say it in 
  871. FORTRAN''. Try to translate each ``Idea'' into FORTRAN, then 
  872. check your answers. If one of your answers is shorter than the 
  873. correct answer, take the test again! For example, the following 
  874. answer to the first idea is wrong:
  875. IF (I .EQ. 2 .OR. 9 .OR. 13)
  876.   END IF FORTRAN 77 lets you say ``END IF''.
  877.   For example, here's how FORTRAN 77 lets you say, ``If I is 
  878. greater than 5, let J be 80 and let K be 90'':
  879.       IF (I .GT. 5) THEN
  880.          J=80
  881.          K=90
  882.       END IF
  883.   Here's how FORTRAN 77 lets you say, ``If I is greater than 5, 
  884. let J be 80 and let K be 90; but if I is not greater than 5, let 
  885. J be 30 and let K be 50'':
  886.       IF (I .GT. 5) THEN
  887.          J=80
  888.          K=90
  889.       ELSE
  890.          J=30
  891.          K=50
  892.       END IF
  893.   Here's how FORTRAN 77 lets you say, ``If I is greater than 5, 
  894. let J be 80 and let K be 90; if I is not greater than 5, but I is 
  895. greater than 2, let J be 81 and let K be 92; if I is not greater 
  896. than 2, let J be 30 and let K be 50'':
  897.       IF (I .GT. 5) THEN
  898.          J=80
  899.          K=90
  900.       ELSE IF (I .GT. 2) THEN
  901.          J=81
  902.          K=92
  903.       ELSE
  904.          J=30
  905.          K=50
  906.       END IF
  907.   Warning: to say ``END IF'', you must get FORTRAN 77. If you use 
  908. FORTRAN IV instead, ``END IF'' doesn't work. I recommend that you 
  909. get FORTRAN 77.
  910.                                                      Three-way IF 
  911. Here's a different kind of IF statement:
  912.       IF (X) 20,50,90
  913. It means:
  914. If X is a negative number, go to line 20.
  915. If X is zero, go to line 50.
  916. If X is a positive number, go to line 90.
  917.                                                      That kind of 
  918. IF statement is called a three-way IF, or an arithmetic IF. (To 
  919. pronounce ``arithmetic'', put the accent on met.) The other kind 
  920. of IF is called a logical IF.
  921.            Computed GO TO
  922.   In your program, you can say:
  923.       GO TO (80,100,20,350), I
  924. That means: go to either 80, 100, 20, or 350, depending on what I 
  925. is. More specifically, it means:
  926. Go to line  80, if I is 1.
  927. Go to line 100, if I is 2.
  928. Go to line  20, if I is 3.
  929. Go to line 350, if I is 4.
  930. Proceed to the line underneath, if I is a different integer.
  931.   That FORTRAN statement is called a computed GO TO. It resembles 
  932. this BASIC statement:
  933. 30 ON I GO TO 80,100,20,350
  934.  
  935.                  DO
  936.   This program prints the square of every number from 80 to 100, 
  937. and then prints GET LOST:
  938. BASIC         FORTRAN
  939. 10 FOR I = 80 TO 100      DO 30 I=80,100
  940. 20     PRINT I^2         J=I**2
  941.                        PRINT 20, J
  942.               20       FORMAT (1X,I14)
  943. 30 NEXT I     30    CONTINUE
  944. 40 PRINT "GET LOST"      PRINT 40
  945.               40    FORMAT (1X,'GET LOST')
  946.                     END
  947.   If you compare the BASIC with the FORTRAN, you'll notice 
  948. FORTRAN uses the word DO instead of FOR, uses a comma instead of 
  949. TO, and uses CONTINUE instead of NEXT. The statement DO 30 I=5,9 
  950. means: DO every line up through line 30, repeatedly, as I goes 
  951. from 5 to 9. In BASIC, programmers indent every line between FOR 
  952. and NEXT; in FORTRAN, programmers indent every line between DO 
  953. and CONTINUE. In BASIC, the indented lines are called a 
  954. FOR...NEXT loop; in FORTRAN, they're called a DO loop.
  955.   If you want the computer to print the square of every fifth 
  956. number from 80 to 100, change the program's top line:
  957. BASIC             FORTRAN
  958. 10 FOR I = 80 TO 100 STEP 5DO 30 I=80,100,5
  959.  
  960.                                          Restrictions In a DO 
  961. statement, some computers allow only positive integer variables 
  962. and constants:
  963. Not allowed                                        Why         
  964. Say this instead
  965. DO 10 X=1.0,5.0                                    reals are not 
  966. allowed                                                        DO 
  967. 10 I=1,5
  968.  
  969. DO 10 X=17.3,98.5                                  reals are not 
  970. allowed                                                        DO 
  971. 10 I=173,985
  972.                                                                   
  973. X=I/10.0
  974.  
  975. DO 10 I=0,5                                        0 is not 
  976. positive                                                       DO 
  977. 10 J=1,6
  978.                                                                   
  979. I=J-1
  980.  
  981. DO 10 I=-3,5                                       -3 is not 
  982. positive                                                       DO 
  983. 10 J=1,9
  984.                                                                   
  985. I=J-4
  986.  
  987. DO 10 I=100,7,-1                                   -1 is not 
  988. positive                                                       DO 
  989. 10 J=7,100
  990.                                                                   
  991. I=107-J
  992.  
  993. DO 10 I=5,J+K                                      + is not 
  994. allowed                                                        
  995. L=J+K
  996.                                                                DO 
  997. 10 I=5,L
  998.                                          Other computers are more 
  999. generous and allow anything. Find out about yours.
  1000.                                          In the middle of a DO 
  1001. loop, don't change the value of the index. For example, if your 
  1002. DO loop begins with ___ 
  1003.       DO 10 I=1,100
  1004. don't insert this line in the middle of your loop:
  1005.          I=14
  1006. It will confuse the computer.
  1007.                                          Zero-trip DO loops If 
  1008. you say ___ 
  1009.       DO 10 I=1,N
  1010. the computer will do up through line 10, N times. For example, if 
  1011. N is 73, the computer will do up through line 10, 73 times. If N 
  1012. is 2, the computer will do up through line 10, twice. If N is 1, 
  1013. the computer will do up through line 10, once.
  1014.                                          What happens if N is 
  1015. less than 1? The answer depends on which version of FORTRAN 
  1016. you're using.
  1017.                                          If you're using FORTRAN 
  1018. 77, the computer will skip the loop, and proceed to the line 
  1019. below line 10. But if you're using FORTRAN IV, the computer will 
  1020. do the loop once, as if N were 1.
  1021.                                          FORTRAN 77 makes more 
  1022. sense; but alas, many computers still use FORTRAN IV.
  1023.                                          A DO loop that FORTRAN 
  1024. 77 skips (because N is less than 1) is called a zero-trip DO 
  1025. loop, because the computer takes ``zero trips through the loop'' 
  1026. (instead of 1 trip or 2 trips or many trips).
  1027.                                          Find out whether your 
  1028. computer's version of FORTRAN resembles FORTRAN 77 and permits 
  1029. zero-trip DO loops.
  1030.  
  1031.                       LISTS
  1032.   To handle lists, use these tricks. . . . 
  1033.  
  1034.                    Subscripts
  1035.   Like BASIC, FORTRAN permits subscripts:
  1036.       DIMENSION X(4)
  1037.       X(1)=.21
  1038.       X(2)=.3
  1039.       X(3)=1.08
  1040.       X(4)=5.0
  1041.       SUM=X(1)+X(2)+X(3)+X(4)
  1042.       PRINT 10, X,SUM
  1043. 10    FORMAT (1X,G14.6)
  1044.       END
  1045.   The top line says X will be a list of 4 numbers, called X(1), 
  1046. X(2), X(3), and X(4). Since X doesn't begin with I, J, K, L, M, 
  1047. or N, the 4 numbers will be real.
  1048.   The PRINT statement makes the computer print the list and the 
  1049. SUM, like this:
  1050.   0.210000
  1051.   0.300000
  1052.    1.08000
  1053.    5.00000
  1054.    6.59000
  1055.   If you change the format to (1X,5G14.6), the computer will 
  1056. print all 5 numbers on the same line:
  1057.   0.210000      0.300000       1.08000       5.00000       
  1058. 6.59000
  1059.   You must say DIMENSION if your program uses subscripts, even if 
  1060. the subscripts are small. Say DIMENSION at the very top of the 
  1061. program. Make sure you say DIMENSION, not DIM or DIMENSIONS. The 
  1062. computer assumes all subscripts will be positive, so don't say 
  1063. X(0). If a subscript is zero or negative or larger than the 
  1064. DIMENSION statement says, the computer might not notice your 
  1065. error, and will print wrong answers without warning you.
  1066.   If your program begins like this ___ 
  1067.       DIMENSION A(6)
  1068.       READ 10, A
  1069. the computer will begin by reading 6 real numbers, which will 
  1070. become A(1), A(2), A(3), A(4), A(5), and A(6).
  1071.   Double subscripts If you want T to be a table of numbers, and 
  1072. you want T to have 4 rows and 2 columns, begin your program by 
  1073. saying:
  1074.       DIMENSION T(4,2)
  1075. To print T, say:
  1076.       PRINT 10, T(1,1), T(1,2)
  1077.       PRINT 10, T(2,1), T(2,2)
  1078.       PRINT 10, T(3,1), T(3,2)
  1079.       PRINT 10, T(4,1), T(4,2)
  1080. 10    FORMAT (1X,2G14.6)
  1081. If you say ___ 
  1082.       PRINT 10, T
  1083. the computer will print the entire table T, but in an undesirable 
  1084. order: it will print T(1,1), T(2,1), T(3,1), and T(4,1), then 
  1085. T(1,2), T(2,2), T(3,2), and T(4,2). Similarly, ``READ 5, T'' 
  1086. makes the computer read T in an undesirable order.
  1087.  
  1088.                    Implied DO
  1089.   This statement ___ 
  1090.       PRINT 10, X(3),X(4),X(5),X(6),X(7)
  1091. can be written more briefly, like this:
  1092.       PRINT 10, (X(I), I=3,7)
  1093. It means: using line 10's format, print the value of X(I), for I 
  1094. = 3 to 7. The construction (X(I), I=3,7) is called an implied DO 
  1095. loop. Notice the parenthesis at the beginning, the parenthesis at 
  1096. the end, and the commas.
  1097.   Here are other examples of implied DO loops:
  1098. Implied DO loopMeaning
  1099. (X(I), I=100,120,5)X(100),X(105),X(110),X(115),X(120)
  1100. (X(I),Y(I), I=3,7)X(3),Y(3), X(4),Y(4), X(5),Y(5), X(6),Y(6), 
  1101. X(7),Y(7)
  1102.   Calendar Here's a calendar:
  1103.  1  2  3  4  5  6  7
  1104.  8  9 10 11 12 13 14
  1105. 15 16 17 18 19 20 21
  1106. 22 23 24 25 26 27 28
  1107. 29 30 31
  1108.   This program prints it:
  1109.       PRINT 10, (I, I=1,31)
  1110. 10    FORMAT (1X,I2,1X,I2,1X,I2,1X,I2,1X,I2,1X,I2,1X,I2)
  1111.       END
  1112.   The program's top statement says to print every value of I, for 
  1113. I = 1 to 31. The FORMAT says to print 7 integers on each line, 
  1114. and separate the integers by spaces.
  1115.   Since 1X followed by I2 is about the same as I3, you can write 
  1116. the FORMAT more briefly:
  1117. 10    FORMAT (I3,I3,I3,I3,I3,I3,I3)
  1118. You can be even briefer:
  1119. 10    FORMAT (7I3)
  1120.   Tables If T is a table having 3 rows and 5 columns, these lines 
  1121. will print it in the correct order:
  1122.       PRINT 20, T(1,1), T(1,2), T(1,3), T(1,4), T(1,5)
  1123.       PRINT 20, T(2,1), T(2,2), T(2,3), T(2,4), T(2,5)
  1124.       PRINT 20, T(3,1), T(3,2), T(3,3), T(3,4), T(3,5)
  1125. 20    FORMAT (1X,5G14.6)
  1126. This short cut does the same thing:
  1127.       PRINT 20, (T(1,J), J=1,5)
  1128.       PRINT 20, (T(2,J), J=1,5)
  1129.       PRINT 20, (T(3,J), J=1,5)
  1130. 20    FORMAT (1X,5G14.6)
  1131. Here's a shorter cut:
  1132.       PRINT 20, ((T(I,J), J=1,5), I=1,3)
  1133. 20    FORMAT (1X,5G14.6)
  1134. To read the table, say:
  1135.       READ 10, ((T(I,J), J=1,5), I=1,3)
  1136.  
  1137.  
  1138.                       DATA
  1139.   This program shows FORTRAN's DATA statement, which differs from 
  1140. BASIC's:
  1141. Program               Meaning
  1142.       DATA X/8.7/, Y/1.4/, Z/9.0/X is 8.7, Y is 1.4, and Z is 
  1143. 9.0.
  1144.       X=100.6         X changes to 100.6
  1145.       PRINT 10, X,Y,Z The computer will print:
  1146. 10    FORMAT (1X,3G14.6)   100.600       1.40000       9.00000
  1147.       END
  1148.   In that DATA statement, you must write 9.0, not 9: the number 
  1149. must be real, since Z is real.
  1150.   The DATA statement resembles these three statements ___ 
  1151.       X=8.7
  1152.       Y=1.4
  1153.       Z=9.0
  1154. but is faster.
  1155.   Here's another way to type the DATA statement:
  1156.       DATA X,Y,Z/8.7,1.4,9.0/
  1157. It says X, Y, and Z are 8.7, 1.4, and 9.0 respectively.
  1158.   Like the DIMENSION statement, the DATA statement belongs at the 
  1159. very top of the program. If you want both a DIMENSION statement 
  1160. and a DATA statement, put the DIMENSION statement first.
  1161.   This DATA statement says A, B, C, D, and E are all 1.7, and X, 
  1162. Y, and Z are all 9.6:
  1163.       DATA A,B,C,D,E,X,Y,Z/1.7,1.7,1.7,1.7,1.7,9.6,9.6,9.6/
  1164. Since the first 5 numbers are 1.7, and the next 3 numbers are 
  1165. 9.6, you can write more briefly:
  1166.       DATA A,B,C,D,E,X,Y,Z/5*1.7,3*9.6/
  1167.   Subscripted DATA To make A be this list ___ 
  1168. 81.7
  1169. 92.6
  1170. 25.3
  1171. 49.8
  1172. 72.1
  1173. 68.8
  1174. begin your program with these lines:
  1175.       DIMENSION A(6)
  1176.       DATA A/81.7,92.6,25.3,49.8,72.1,68.8/
  1177.   To make T be this table ___ 
  1178. 8.4 9.7
  1179. 5.1 6.8
  1180. 2.5 7.2
  1181. 6.3 9.8
  1182. begin your program with these lines:
  1183.       DIMENSION T(4,2)
  1184.       DATA T/8.4,5.1,2.5,6.3,9.7,6.8,7.2,9.8/
  1185. Notice you must list the entire first column, then the second.
  1186.  
  1187.           String variables
  1188.   This program works on most computers:
  1189.       N='UP'
  1190.       PRINT 10, N
  1191. 10    FORMAT (1X,A2)
  1192.       END
  1193. The top line says N is the string `UP'. The next line says to 
  1194. print N, using the format in line 10. The A2 format means a 
  1195. 2-character string. (The A is derived from the word Alphabet.) 
  1196. The computer will print:
  1197. UP
  1198.   Different versions Some computers allow apostrophes only in 
  1199. FORMAT, DATA, and CALL statements. (You'll learn about CALL 
  1200. statements later.) On such computers, the statement N=`UP' is 
  1201. illegal. Instead say:
  1202.       DATA N/'UP'/
  1203.   Restrictions A string statement should look like an integer: it 
  1204. should begin with the letter I, J, K, L, M, or N. You can say 
  1205. N=`UP' but shouldn't say X=`UP'.
  1206.   The string a variable stands for must be short. You  can  say 
  1207. N=`UP' but not N= `SUPERCALIFRAGILISTICEXPIALIDOCIOUS'. The 
  1208. longest permissible string depends on your computer:
  1209. Computer            Longest string allowed
  1210. PDP-11 (using FORTRAN IV) 2 characters
  1211. PDP-11 (using FORTRAN IV-PLUS), IBM 4 characters
  1212. PDP-10, PDP-20, Honeywell 5 characters
  1213. CDC                   10 characters
  1214.   Fancy examples Examine this program:
  1215.       PRINT 10
  1216. 10    FORMAT (1X,'DO YOU LIKE ME?')
  1217.       READ 20, IREPLY
  1218. 20    FORMAT (A1)
  1219.       IF (IREPLY .EQ. 'Y') PRINT 30
  1220. 30    FORMAT (1X,'I LIKE YOU TOO!')
  1221.       PRINT 40
  1222. 40    FORMAT (1X,'SO LONG, BUSTER.')
  1223.       END
  1224.   The first pair of lines make the computer print DO YOU LIKE ME? 
  1225. The next pair set IREPLY equal to the first letter the human 
  1226. types. If the human types YESIREE, the computer will set IREPLY 
  1227. equal to `Y' and will therefore print:
  1228. I LIKE YOU TOO!
  1229. SO LONG, BUSTER.
  1230. But if the human types NOT AT ALL, the computer will set IREPLY 
  1231. equal to `N' and will therefore print just:
  1232. SO LONG, BUSTER.
  1233. In that program, the string is called IREPLY instead of REPLY, to 
  1234. make it an integer.
  1235.                                          Advanced example:
  1236.       DIMENSION NAME(25)
  1237.       PRINT 10
  1238. 10    FORMAT (1X,'WHAT IS YOUR NAME?')
  1239.       READ 20, NAME
  1240. 20    FORMAT (25A2)
  1241.       PRINT 30, NAME
  1242. 30    FORMAT (1X,'I HATE ANYONE NAMED ',25A2)
  1243.       END
  1244. The top line says NAME will be a list of 25 elements. The next 
  1245. pair of lines print WHAT IS YOUR NAME? If the human answers ___ 
  1246. BARTHOLOMEW HIERONYMOUS MCGILLICUDDY, M.D.
  1247. the format in line 20 sets NAME equal to 25 two-character 
  1248. strings:
  1249. NAME(1) is `BA'                                  NAME(10) is 
  1250. `YM'NAME(19) is `, '
  1251. NAME(2) is `RT'                                  NAME(11) is 
  1252. `OU'NAME(20) is `M.'
  1253. NAME(3) is `HO'                                  NAME(12) is `S 
  1254. 'NAME(21) is `D.'
  1255. NAME(4) is `LO'                                  NAME(13) is 
  1256. `MC'NAME(22) is `  '
  1257. NAME(5) is `ME'                                  NAME(14) is 
  1258. `GI'NAME(23) is `  '
  1259. NAME(6) is `W '                                  NAME(15) is 
  1260. `LL'NAME(24) is `  '
  1261. NAME(7) is `HI'                                  NAME(16) is 
  1262. `IC'NAME(25) is `  '
  1263. NAME(8) is `ER'                                  NAME(17) is `UD'
  1264. NAME(9) is `ON'                                  NAME(18) is `DY'
  1265. Format 30 prints:
  1266. I HATE ANYONE NAMED BARTHOLOMEW HIERONYMOUS MCGILLICUDDY, M.D.
  1267.                                          To make that program run 
  1268. faster, use fewer strings. For example, if you have a CDC 
  1269. computer, each string can be as long as 10 characters, so you 
  1270. need only 5 strings to make 50 characters:
  1271.       DIMENSION NAME(5)
  1272.       PRINT 10
  1273. 10    FORMAT (1X,'WHAT IS YOUR NAME?')
  1274.       READ 20, NAME
  1275. 20    FORMAT (5A10)
  1276.       PRINT 30, NAME
  1277. 30    FORMAT (1X,'I HATE ANYONE NAMED ',5A10)
  1278.       END
  1279.                                          FORTRAN 77 strings On 
  1280. computer having FORTRAN 77, and on IBM computers using WATFIV, 
  1281. you can request super-long strings:
  1282.       CHARACTER*50 NAME
  1283.       PRINT 10
  1284. 10    FORMAT (1X,'WHAT IS YOUR NAME?')
  1285.       READ 20, NAME
  1286. 20    FORMAT (A50)
  1287.       PRINT 30, NAME
  1288. 30    FORMAT (1X,'I HATE ANYONE NAMED ',A50)
  1289.       END
  1290.                                          The top line requests 
  1291. that NAME be a 50-character string. Like the DIMENSION statement, 
  1292. the CHARACTER statement must be put at the very top of the 
  1293. program, above even the DATA statements.
  1294.  
  1295.               FUNCTIONS
  1296.   To do advanced math, use FORTRAN's functions.
  1297.  
  1298.              Square root
  1299.   This program finds the square root of 9:
  1300.       A=SQRT(9.0)
  1301.       PRINT 10, A
  1302. 10    FORMAT (1X,G14.6)
  1303.       END
  1304. The computer will print:
  1305.    3.00000
  1306.   In that program, you must say SQRT(9.0), not SQRT(9). If you 
  1307. say SQRT(9), the computer will either gripe or print a wrong 
  1308. answer.
  1309.   The number in parentheses must be real. That's why you can say 
  1310. SQRT(9.0) but not SQRT(9). You can say SQRT(8.0+1.0) but not 
  1311. SQRT(8+1). You can say SQRT(X) but not SQRT(J).
  1312.   Be careful when you translate from BASIC to FORTRAN: in BASIC, 
  1313. you say SQR; in FORTRAN, you say SQRT instead.
  1314.   This program prints a table, showing the square root of 2.0, 
  1315. the square root of 3.0, the square root of 4.0, the square root 
  1316. of 5.0, etc.:
  1317.       X=2.0
  1318. 10    Y=SQRT(X)
  1319.       PRINT 20, X,Y
  1320. 20    FORMAT (1X,2G14.6)
  1321.       X=X+1.0
  1322.       GO TO 10
  1323.       END
  1324. The computer will print:
  1325.    2.00000       1.41421
  1326.    3.00000       1.73205
  1327.    4.00000       2.00000
  1328.    5.00000       2.23607
  1329.    6.00000       2.44949
  1330.    7.00000       2.64575
  1331.    8.00000       2.82843
  1332.    9.00000       3.00000
  1333.    10.0000       3.16228
  1334.    etc.
  1335.  
  1336.                 FLOAT
  1337.   If you say FLOAT, the computer will create a FLOATing-point 
  1338. number (in other words, a real number), by using an integer. For 
  1339. example, FLOAT(3) is 3.0. If J is 7, then FLOAT(J) is 7.0.
  1340.   The word FLOAT can help you solve the following problems. . . . 
  1341.   Find the square root of an integer J Unfortunately, you aren't 
  1342. allowed to say SQRT(J), because what you take the square root of 
  1343. must be a real number. You can say SQRT(X) but not SQRT(J). 
  1344. Solution: say X=J, and then say SQRT(X). Shorter solution: say 
  1345. SQRT(FLOAT(J)).
  1346.   Divide J by K accurately Unfortunately, saying J/K gives an 
  1347. inaccurate answer, because when the computer divides integers it 
  1348. gives an integer answer, instead of an accurate real answer. 
  1349. Solution: say X=J, then Y=K, then X/Y. Shorter solution: say 
  1350. FLOAT(J)/FLOAT(K).
  1351.                                                   Random numbers
  1352.                                          Here's how to set R 
  1353. equal to a random decimal between 0 and 1:
  1354. Computer                                         What to say
  1355. CDC                                              R=RANF(0)
  1356. PDP-10, PDP-11                                   R=RAN(0)
  1357. PDP-11                                           
  1358. R=RAN(ISEED,ISEED2)
  1359.                                          To randomize the random 
  1360. numbers on PDP-10 and PDP-20 computers, put these lines near the 
  1361. top of your program:
  1362.       CALL TIME(ISEED,ISEED2)
  1363.       CALL SETRAN(MOD(ISEED/2+ISEED2/2, 2147483648))
  1364. On other computers, randomizing is even more complicated; ask the 
  1365. people who run your computer center.
  1366.  
  1367.                                                  Maxima and minima
  1368.                                          To find the maximum real 
  1369. number in a list, ask for AMAX1.
  1370.                                          For example, AMAX1(4.7, 
  1371. 2.8, 41.6, 9.2, 82.3, 9.7) is 82.3. And AMIN1(4.7, 2.8, 41.6, 
  1372. 9.2, 82.3, 9.7) is the minimum, which is 2.8.
  1373.                                          If the numbers in the 
  1374. list are integers, say MAX0 instead of AMAX1, and say MIN0 
  1375. instead of AMIN1. When you type ``MAX0'' and ``MIN0'', make sure 
  1376. you end with a zero, not the letter ``oh''.
  1377.  
  1378.                                                   Absolute value
  1379.                                          ABS(X) means the 
  1380. ABSolute value of X; in other words, X without its minus sign. 
  1381. For example:
  1382. ABS(-5.2) is 5.2
  1383. ABS(-7.0) is 7.0
  1384. ABS(9.3)  is 9.3
  1385.                                          For integers, say IABS 
  1386. instead of ABS. For example, IABS(-7) is 7.
  1387.  
  1388.                                                      Remainder
  1389.                                          When you divide 11 by 4, 
  1390. the remainder is 3:
  1391.      2
  1392. 4 ) 11
  1393.     -8
  1394.      3 is the remainder
  1395.                                          If you ask for MOD(11,4) 
  1396. the computer will divide 11 by 4 and get the remainder, which is 
  1397. 3; so MOD(11,4) is 3.
  1398.                                          Use MOD for integers; 
  1399. use AMOD for reals. For example, AMOD(11.0, 4.0) is 3.0.
  1400.                   Trigonometry
  1401.   If your computer has FORTRAN 77, or your computer is an IBM 
  1402. having FORTRAN H-EXTENDED, or your computer is a PDP-11 having 
  1403. FORTRAN IV-PLUS, you can use these trigonometric functions:
  1404. SymbolMeaning
  1405. SIN(X)the SINe of X radians
  1406. COS(X)the COSine of X radians
  1407. TAN(X)the TANgent of X radians
  1408.  
  1409. ASIN(X)the ArcSINe of X in radians; the number whose sine is X
  1410. ACOS(X)the ArcCOSine of X in radians; the number whose cosine is 
  1411. X
  1412. ATAN(X)the ArcTANgent of X in radians; the number whose tangent 
  1413. is X
  1414.  
  1415. SINH(X)the SINe Hyperbolic of X
  1416. COSH(X)the COSine Hyperbolic of X
  1417. TANH(X)the TANgent Hyperbolic of X
  1418.   If your computer is old-fashioned, it restricts you:
  1419. Old-fashioned systemRestriction
  1420. PDP-10, PDP-20  you can't say TAN(X)
  1421. CDC             you can't say SINH(X) or COSH(X)
  1422. other IBM computerssay ARSIN(X), not ASIN(X); say ARCOS(X), not 
  1423. ACOS(X)
  1424. FORTRAN IV computersyou can't say TAN(X), ASIN(X), ACOS(X), 
  1425. SINH(X), COSH(X)
  1426.   You can replace X by any real number. For example, you can say 
  1427. SIN(4.0) but not SIN(4); you can say SIN(Y) but not SIN(J).
  1428.   Be careful when you translate from BASIC to FORTRAN: in BASIC, 
  1429. you say ATN; in FORTRAN, you say ATAN instead.
  1430.   You've seen that SIN(X) is the sine of X radians. But what's 
  1431. the sine of X degrees? On PDP-10 and PDP-20 computers, you can 
  1432. find the sine of X degrees by asking for SIND(X); and you can 
  1433. find the cosine of X degrees by asking for COSD(X).
  1434.   ATAN2(Y,X) is about the same as ATAN(Y/X), but is faster, more 
  1435. accurate, and gives a useful answer even when X is zero or 
  1436. negative. ATAN2(Y,X) is the angle (in radians) of the line that 
  1437. goes through the origin and the point (X,Y).
  1438.  
  1439.                     Calculus
  1440.   You can use these functions:
  1441. FunctionMeaning
  1442.  
  1443. EXP(X)  eX
  1444.  
  1445. ALOG(X) loge X
  1446.  
  1447. ALOG10(X)log10 X
  1448.   You can replace X by any real number, but not by an integer. 
  1449. Each of those functions produces a real answer, since none of 
  1450. them begins with I, J, K, L, M, or N.
  1451.   The logarithm function is called ALOG instead of LOG, to avoid 
  1452. beginning with L. Be careful when you translate from BASIC to 
  1453. FORTRAN: in BASIC, you say LOG; in FORTRAN, you say ALOG instead.
  1454.  
  1455.  
  1456.                  EXOTIC FEATURES
  1457.   Let's take off our handcuffs and go wild!
  1458.   Let's go beyond integers and reals, to other kinds of numbers 
  1459. that are wilder: double precision and complex.
  1460.   Let's go beyond standard functions and invent our own 
  1461. functions. Let's go beyond standard statements and invent our own 
  1462. statements, by using subroutines and comments. Let's go beyond 
  1463. the DATA statement and invent data files.
  1464.   Here we go. . . . 
  1465.  
  1466.                     Comments
  1467.   If you type C instead of a line number, the computer will 
  1468. ignore the line.
  1469.       N=50+13
  1470. C  I HATE COMPUTERS!
  1471.       PRINT 10, N
  1472. 10    FORMAT (1X,I14)
  1473.       END
  1474. The computer will ignore the Comment. The computer will print 63.
  1475.   The C in FORTRAN is like the REMARK in BASIC: use it to 
  1476. document your program. 
  1477.  
  1478.                 DOUBLE PRECISION
  1479.   Some computers are more accurate than others:
  1480. Computer      Accuracy for real numbers
  1481. PDP-11, IBM    7 digits
  1482. PDP-10, PDP-20, Honeywell 8 digits
  1483. CDC           14 digits
  1484.   For example, suppose you feed this program to a PDP-11 or IBM:
  1485.       A=5398.1642376236
  1486.       PRINT 10, A
  1487. 10    FORMAT (1X,F15.10)
  1488.       END
  1489. Expect the first 7 digits the computer prints to be correct 
  1490. (5398.164), but the remaining digits it prints to be wrong: they 
  1491. arise from round-off error inside the computer.
  1492.   A PDP-11 or IBM prints some numbers to an accuracy of 8 digits 
  1493. and others to an accuracy of just 6 digits, but 7 digits is 
  1494. typical.
  1495.   For real numbers, I recommend you use a G14.6 format, because 
  1496. it's safe: it prints just the first 6 digits. If you want to see 
  1497. further digits that are probably correct, use these formats 
  1498. instead:
  1499. Computer      FormatWhat the format does
  1500. PDP-11, IBM   G15.7 prints the first  7 digits
  1501. PDP-10, PDP-20, HoneywellG16.8prints the first  8 digits
  1502. CDC           G22.14prints the first 14 digits
  1503.   You can obtain extra accuracy, by requesting double precision:
  1504. Computer          Accuracy for double precisionFormat
  1505. PDP-11            14 digits             D22.14
  1506. PDP-10 (using KA), Honeywell16 digits   D24.16
  1507. IBM               17 digits             D25.17
  1508. PDP-10 (using KI or KL), PDP-2018 digitsD26.18
  1509. CDC               29 digits             D37.29
  1510.   Each of these programs computes the square root of 6.3x108 on a 
  1511. PDP-11 computer:
  1512. Using reals     Using double precision
  1513.                       DOUBLE PRECISION A
  1514.       A=SQRT(6.3E8)      A=DSQRT(6.3D8)
  1515.       PRINT 10, A      PRINT 10, A
  1516. 10    FORMAT (1X,G15.7)10    FORMAT (1X,D22.14)
  1517.       END             END
  1518. The program on the left computes 7 digits. The program on the 
  1519. right computes 14. Comparing the programs, you'll notice four 
  1520. differences:
  1521.                                                      1. For 
  1522. double precision you must use double precision numbers. Instead 
  1523. of saying 6.3E8, say 6.3D8. The D means Double precision.
  1524. Real number                                                  
  1525. Double precision
  1526. 6.3E8                                                        
  1527. 6.3D8
  1528. 29.6                                                         
  1529. 29.6D0
  1530.                                                                   
  1531.                                                                   
  1532. this is a zero
  1533.                                                      2. For 
  1534. double precision, you must use double precision functions. 
  1535. Instead of saying SQRT, say DSQRT.
  1536. Real function                                                
  1537. Double precision
  1538. SQRT                                                         
  1539. DSQRT
  1540.  
  1541. AMAX1                                                        
  1542. DMAX1
  1543. AMIN1                                                        
  1544. DMIN1
  1545.  
  1546. ABS                                                          DABS
  1547. AMOD                                                         DMOD
  1548. EXP                                                          DEXP
  1549.  
  1550. ALOG                                                         DLOG
  1551. ALOG10                                                       
  1552. DLOG10
  1553.  
  1554. SIN                                                          DSIN
  1555. COS                                                          DCOS
  1556. TAN                                                          DTAN
  1557.  
  1558. ASIN                                                         
  1559. DASIN
  1560. ACOS                                                         
  1561. DACOS
  1562. ATAN                                                         
  1563. DATAN
  1564. ATAN2                                                        
  1565. DATAN2
  1566.  
  1567. SINH                                                         
  1568. DSINH
  1569. COSH                                                         
  1570. DCOSH
  1571. TANH                                                         
  1572. DTANH
  1573.                                                      3. For 
  1574. double precision, you must use double precision variables. 
  1575. Normally, the variable A would be real; to make it double 
  1576. precision instead, say:
  1577.       DOUBLE PRECISION A
  1578. Normally, the variables LENGTH and MASS would be integers, and 
  1579. SPEED would be real; to make them all double precision, say:
  1580.       DOUBLE PRECISION LENGTH,MASS,SPEED
  1581. Like the DIMENSION and CHARACTER statements, the DOUBLE PRECISION 
  1582. statement must be put at the very top of the program, above even 
  1583. the DATA statements. If you say ___ 
  1584.       IMPLICIT DOUBLE PRECISION(D)
  1585. every variable whose first letter is D will automatically be 
  1586. double precision; for example, DISTAN and DIAMET and DSIZE will 
  1587. automatically be double precision.
  1588.                                                      4. For 
  1589. double precision you must use double precision formats. Instead 
  1590. of a G format, use D22.14, or whichever D format is appropriate 
  1591. for your computer.
  1592.                                                      Expense 
  1593. Although double precision arithmetic is more precise than real 
  1594. arithmetic, it's also more expensive: it consumes more of the 
  1595. computer's time, and the numbers consume more of the computer's 
  1596. memory.
  1597.   Combinations If you combine an integer or a real number with a 
  1598. double precision number, the answer will be double precision.
  1599.  
  1600.                COMPLEX
  1601.   In mathematics, the square root of -1 is called i. So i2 is -1. 
  1602. The number i obeys most of the rules of algebra:
  1603.  i+i  is 2i
  1604. 2i+3i is 5i
  1605. (8+2i) + (7+3i) is 15+5i
  1606. (8+2i) * (7+3i) is 8*7 + 8*3i + 2i*7 + 2i*3i,
  1607.           which is 56 + 24i + 14i + 6i2,
  1608.           which is 56 + 24i + 14i + -6,
  1609.           which is 50 + 38i
  1610.   The number i is neither positive nor negative nor zero; it's 
  1611. pictured instead as being above the real number line:
  1612.                 i
  1613. -5 -4 -3 -2 -1  0  1  2  3  4  5
  1614. 2i is further above the real number line: it's 2 units above 0. 
  1615. Another example: 5+2i is 2 units above 5.
  1616.   A number that involves i is called complex. So 5+2i is complex. 
  1617. Its real part is 5, its imaginary part is 2, and its conjugate is 
  1618. 5-2i.
  1619.   This program multiplies 8+2i by 7+3i and prints the correct 
  1620. answer, 50+38i:
  1621.       COMPLEX B
  1622.       B=(8.0, 2.0) * (7.0, 3.0)
  1623.       PRINT 10, B
  1624. 10    FORMAT (1X,2G14.6)
  1625.       END
  1626.   FORTRAN says (8.0, 2.0) instead of 8+2i. The decimal points and 
  1627. parentheses are required.
  1628.   To make B complex instead of real, say COMPLEX B. Like the 
  1629. DIMENSION and CHARACTER and DOUBLE PRECISION statements, the 
  1630. COMPLEX statement must be put at the very top of the program, 
  1631. above even the DATA statements.
  1632.   To print B, use the G14.6 format twice (once for the real part, 
  1633. and once for the imaginary part). The computer will print:
  1634.    50.0000       38.0000
  1635.   If you say IMPLICIT COMPLEX(C), every variable whose first 
  1636. letter is C will automatically be complex.
  1637.   Although you can write 8+2i as (8.0, 2.0), you cannot write 
  1638. X+Yi as (X, Y); instead write CMPLX(X, Y).
  1639.   Here's the rule: to build a complex number from variables 
  1640. instead of from constants, say CMPLX. The variables that the 
  1641. complex number is built from must be real.
  1642.   If B is complex, you can use these functions:
  1643. FunctionMeaning
  1644. CSQRT(B)the complex number that's the square root of B
  1645.  
  1646. CSIN(B) the complex number that's the sine of B
  1647. CCOS(B) the complex number that's the cosine of B
  1648.  
  1649. CEXP(B) the complex number that's eB
  1650. CLOG(B) the complex number that's log B
  1651.  
  1652. CABS(B) the real number that's the absolute value of B
  1653. REAL(B) the real number that's the real part of B
  1654. AIMAG(B)the real number that's the imaginary part of B
  1655.  
  1656. CONJG(B)the complex number that's the conjugate of B
  1657.  
  1658.                                          This program finds the 
  1659. square root of -9 and prints the correct answer, 3i:
  1660.       COMPLEX B,Z
  1661.       B=-9
  1662.       Z=CSQRT(B)
  1663.       PRINT 10, Z
  1664. 10    FORMAT (1X,2G14.6)
  1665.       END
  1666. Since the top line says B is complex, the second line makes B the 
  1667. complex number -9+0i. The next line makes Z the square root of 
  1668. -9+0i, which is 0+3i. The computer will print the 0 and the 3:
  1669.    0.000000       3.00000
  1670.                                          Since that program sets 
  1671. B equal to -9+0i, which FORTRAN writes as (-9.0, 0.0), you can 
  1672. shorten the program to this:
  1673.       COMPLEX Z
  1674.       Z=CSQRT( (-9.0, 0.0) )
  1675.       PRINT 10, Z
  1676. 10    FORMAT (1X,2G14.6)
  1677.       END
  1678. Make sure the number inside CSQRT's parentheses is complex: you 
  1679. need the decimal points, comma, and parentheses.
  1680.                                          When the computer 
  1681. combines a complex number with an integer, a real, or a double 
  1682. precision number, the result is complex.
  1683.                                          Some computers limit 
  1684. your use of complex numbers, by restrictions such as:
  1685. ``Don't say a complex number equals a double precision number.''
  1686. ``Don't combine a complex number with a double precision 
  1687. number.''
  1688. ``Don't raise a number to a complex power.''
  1689. ``Don't raise a complex number to a power, unless the power is an 
  1690. integer.''
  1691. Find out whether your computer has those restrictions.
  1692.              Subroutines
  1693.   This program is a combination of two routines:
  1694.       PRINT 10
  1695. 10    FORMAT (1X,'KIDS')
  1696.       CALL YUMMY
  1697.       PRINT 20
  1698. 20    FORMAT (1X,'LOLLIPOPS')
  1699.       END
  1700.  
  1701.       SUBROUTINE YUMMY
  1702.       PRINT 10
  1703. 10    FORMAT (1X,'SUCK')
  1704.       RETURN
  1705.       END
  1706.   Each routine ends with the word END.
  1707.   The first routine is called the main routine.
  1708.   The second routine is called the subroutine, and begins with 
  1709. the word SUBROUTINE. I decided to name it YUMMY, so its top line 
  1710. says SUBROUTINE YUMMY.
  1711.   Above the word SUBROUTINE, I put a blank line. That blank line 
  1712. is optional; it helps humans find where the subroutine begins.
  1713.   In the main routine, the top pair of lines makes the computer 
  1714. print:
  1715. KIDS
  1716. The next line says CALL YUMMY, which makes the computer skip down 
  1717. to subroutine YUMMY. Subroutine YUMMY makes the computer print 
  1718. ___ 
  1719. SUCK
  1720. and then RETURN to the main routine, which finishes by printing:
  1721. LOLLIPOPS
  1722.   Altogether, the program prints:
  1723. KIDS
  1724. SUCK
  1725. LOLLIPOPS
  1726.   Notice that line 10 in the main routine is different from line 
  1727. 10 in the subroutine. Similarly, an X in the main routine is 
  1728. different from one in the subroutine:
  1729.       X=3.4
  1730.       CALL FUNNY
  1731.       PRINT 10,X
  1732. 10    FORMAT (1X,G14.6)
  1733.       END
  1734.  
  1735.       SUBROUTINE FUNNY
  1736.       X=925.1
  1737.       Y=X+1.0
  1738.       PRINT 10, Y
  1739. 10    FORMAT (1X,G14.6)
  1740.       RETURN
  1741.       END
  1742. The computer will set the main routine's X equal to 3.4. Then it 
  1743. will call FUNNY. The X in FUNNY is 925.1, so Y is 926.1, and the 
  1744. computer will print:
  1745.    926.100
  1746. When the computer returns to the main routine, it will print the 
  1747. main routine's X, which is still:
  1748.    3.40000
  1749.  
  1750.                                          Passing information 
  1751. between routines To pass information from one routine to another, 
  1752. put the information in parentheses:
  1753.       A=5.2
  1754.       CALL LEMON(A)
  1755.       PRINT 10, A
  1756. 10    FORMAT (1X,G14.6)
  1757.       END
  1758.  
  1759.       SUBROUTINE LEMON(X)
  1760.       PRINT 100, X
  1761. 100   FORMAT (1X,G14.6)
  1762.       X=7.1
  1763.       RETURN
  1764.       END
  1765. The computer sets A equal to 5.2. Then it calls LEMON. The A and 
  1766. X in parentheses mean: the main routine's A is the subroutine's 
  1767. X, so the subroutine's X is 5.2. Line 100 prints:
  1768.    5.20000
  1769. The next line changes X to 7.1. When the computer returns to the 
  1770. main routine, the main routine's A is the subroutine's X, so the 
  1771. main routine's A is 7.1. Line 10 prints:
  1772.    7.10000
  1773.                                          A harder example:
  1774.       P=2.1
  1775.       CALL JUNK(P)
  1776.       PRINT 10, P
  1777. 10    FORMAT (1X,G14.6)
  1778.       END
  1779.  
  1780.       SUBROUTINE JUNK(P)
  1781.       P=3.0*P
  1782.       RETURN
  1783.       END
  1784. The computer sets P equal to 2.1 and then calls JUNK. The P in 
  1785. parentheses means: the main routine's P is the subroutine's P. 
  1786. The subroutine triples P, so P becomes 6.3. When the computer 
  1787. returns to the main routine, line 10 prints:
  1788.    6.30000
  1789.                                          Another example:
  1790.       Q=1.4
  1791.       CALL FAT(5.0, Q+.3, R)
  1792.       PRINT 10, R
  1793. 10    FORMAT (1X,G14.6)
  1794.       END
  1795.  
  1796.       SUBROUTINE FAT(X, Y, Z)
  1797.       Z=X+Y
  1798.       RETURN
  1799.       END
  1800. When the main routine calls FAT, the subroutine's X is 5.0; Y is 
  1801. Q+.3, which is 1.7; and Z is R, which is undefined. The 
  1802. subroutine sets Z equal to X+Y, which is 6.7. When the computer 
  1803. returns to the main routine, the main routine's R is the 
  1804. subroutine's Z, which is 6.7. Line 10 prints:
  1805.    6.70000
  1806.                                          In that CALL, you must 
  1807. say 5.0, not 5, since X must be real. Saying 5 will confuse the 
  1808. computer and make it print a wrong answer.
  1809.   Sum & average of a trio This subroutine finds the sum and 
  1810. average of three real numbers ___ X, Y, and Z:
  1811.       SUBROUTINE STAT(X, Y, Z, SUM, AVERAG)
  1812.       SUM=X+Y+Z
  1813.       AVERAG=SUM/3.0
  1814.       RETURN
  1815.       END
  1816.   This main routine uses STAT to find the sum and average of 8.1, 
  1817. 2.6, and 9.3:
  1818.       CALL STAT(8.1, 2.6, 9.3, SUM, AVERAG)
  1819.       PRINT 10, SUM,AVERAG
  1820. 10    FORMAT (1X,2G14.6)
  1821.       END
  1822.   This subroutine finds the sum and average of three double 
  1823. precision numbers:
  1824.       SUBROUTINE DSTAT(X, Y, Z, SUM, AVERAG)
  1825.       DOUBLE PRECISION X, Y, Z, SUM, AVERAG
  1826.       SUM=X+Y+Z
  1827.       AVERAG=SUM/3D0
  1828.       RETURN
  1829.       END
  1830. This main routine uses DSTAT to find the sum and average of π, e, 
  1831. and my phone number (6662666):
  1832.       DOUBLE PRECISION SUM, AVERAG
  1833.       CALL DSTAT(3.1415926535898D0, 2.7182818284590D0, 6662666D0, 
  1834. SUM,
  1835.      6AVERAG)
  1836.       PRINT 10, SUM, AVERAG
  1837. 10    FORMAT (1X,2D22.14)
  1838.       END
  1839. You must say DOUBLE PRECISION in both the main routine and the 
  1840. subroutine.
  1841.   Sum & average of a long list This subroutine finds the sum and 
  1842. average of X1, X2, X3, . . . , XN:
  1843.       SUBROUTINE STAT2(X, N, SUM, AVERAG)
  1844.       DIMENSION X(N)
  1845.       SUM=0.0
  1846.       DO 10 I=1,N
  1847.          SUM=SUM+X(I)
  1848. 10    CONTINUE
  1849.       AVERAG=SUM/N
  1850.       RETURN
  1851.       END
  1852.   This main routine uses STAT2 to find the sum and average of 
  1853. 8.4, 9.6, 20.1, 7.2, 91.5, and 3.6:
  1854.       DIMENSION X(6)
  1855.       DATA X/8.4,9.6,20.1,7.2,91.5,3.6/
  1856.       CALL STAT2(X, 6, SUM, AVERAG)
  1857.       PRINT 10, SUM,AVERAG
  1858. 10    FORMAT (1X,2G14.6)
  1859.       END
  1860.   You must put the DIMENSION statement in both the main routine 
  1861. and the subroutine. In the main routine, the DIMENSION must be a 
  1862. constant (6); in the subroutine, the DIMENSION can be a variable 
  1863. (N).
  1864.   This main routine asks you to input some numbers, then prints 
  1865. their sum and average by using STAT2:
  1866.       DIMENSION X(100)
  1867.       PRINT 10
  1868. 10    FORMAT (1X,'HOW MANY NUMBERS WOULD YOU LIKE TO GIVE ME?')
  1869.       READ *, N
  1870.       PRINT 20
  1871. 20    FORMAT (1X,'TYPE THE NUMBERS')
  1872.       READ *, (X(I), I=1,N)
  1873.       CALL STAT(X, N, SUM, AVERAG)
  1874.       PRINT 10, SUM,AVERAG
  1875. 10    FORMAT (1X,'THE SUM IS',G14.6,' AND THE AVERAGE IS',G14.6)
  1876.       END
  1877. That DIMENSION statement lets you input up to 100 numbers.
  1878.                                                      If a main 
  1879. routine says DIMENSION X(100), and X is passed between the main 
  1880. routine and the subroutine, the subroutine's DIMENSION statement 
  1881. must say no more than X(100): if the subroutine's DIMENSION 
  1882. statement says X(N), the N must be no more than 100.
  1883.                                                      Passing 
  1884. double subscripts For double subscripts, the main routine's 
  1885. DIMENSION statement must be exactly the same as the subroutine's.
  1886.                                                      For example, 
  1887. suppose the main routine says DIMENSION X(25,4). Then the 
  1888. subroutine must say DIMENSION X(25,4) also. The subroutine must 
  1889. not say DIMENSION X(20,3). If the subroutine says DIMENSION 
  1890. X(M,N), the M must be exactly 25.
  1891.                                                      Famous 
  1892. subroutines The Scientific Subroutine Package (SSP) is a 
  1893. collection of subroutines written by IBM that do statistics, 
  1894. calculus, equation-solving, and advanced math. The Calcomp 
  1895. subroutines make the computer operate a Calcomp plotter (a device 
  1896. that draws fancy shapes on paper, by using a felt-tip or 
  1897. ballpoint pen).
  1898.                                                      Most large 
  1899. computers store the SSP and Calcomp subroutines on disk 
  1900. permanently. Ask the people in your computer center how to 
  1901. combine your own main routines with those subroutines, and how to 
  1902. put your own library of subroutines onto the disk.
  1903.                                                      How to write 
  1904. a big program If you and your friends want to write a big program 
  1905. together, divide the problem into a main routine and several 
  1906. subroutines, and have each person write one routine.
  1907.                                                      Although 
  1908. you'll need a group conference to decide what the SUBROUTINE and 
  1909. CALL statements will be, you don't have to agree on the names of 
  1910. variables, since the names of subroutine variables have nothing 
  1911. to do with the names of main-routine variables.
  1912.          Your own functions
  1913.   Instead of using functions such as SQRT and ABS, you can invent 
  1914. your own functions. Here's how to invent a function called F, and 
  1915. how to use the function you've invented:
  1916.       A=F(5.2)+1.0
  1917.       PRINT 10, A
  1918. 10    FORMAT (1X,G14.6)
  1919.       END
  1920.  
  1921.       FUNCTION F(X)
  1922.       F=2.0*X
  1923.       RETURN
  1924.       END
  1925.   The program consists of two routines. The first routine is the 
  1926. main routine. The second routine is the definition of the 
  1927. FUNCTION F. Each routine ends with the word END.
  1928.   The main routine's top line requires the computer to find 
  1929. F(5.2), so the computer hunts for the definition of FUNCTION F. 
  1930. The definition says F is twice 5.2, which is 10.4. So A is 11.4. 
  1931. The computer will print:
  1932.    11.40000
  1933.   Like a subroutine, a function definition can be very long and 
  1934. have many variables in parentheses.
  1935.   Naming your function You can give your function the name F or G 
  1936. or any other name, such as MASS. If the function's name begins 
  1937. with I, J, K, L, M, or N, the computer will assume its value is 
  1938. an integer.
  1939.   If you want the function MASS(X) to be double precision instead 
  1940. of an integer, begin your main routine by saying ___ 
  1941.       DOUBLE PRECISION MASS
  1942. and begin your function definition with this line:
  1943.       DOUBLE PRECISION FUNCTION MASS(X)
  1944.  
  1945.                                                        Files
  1946.                                          The computer can write 
  1947. its answers onto a data file:
  1948.       A=14.6+75.2
  1949.       WRITE(3,10) A
  1950. 10    FORMAT (1X,G14.6)
  1951.       WRITE(7,20)
  1952. 20    FORMAT (1X,'TINA, PLEASE TICKLE MY TUBA')
  1953.       END
  1954. Since A is 89.8, the second line makes the computer write 89.8 
  1955. onto ``file 3'', using line 10's format. The next pair of lines 
  1956. make the computer write onto ``file 7'', using line 20's format; 
  1957. so the computer will write TINA, PLEASE TICKLE MY TUBA onto file 
  1958. 7.
  1959.                                          Before running that 
  1960. program, tell the computer where to put files 3 and 7. You can 
  1961. make the computer put them on a disk, line printer, tape, 
  1962. terminal, or wherever else you please. Ask the people in your 
  1963. computer center how to tell the computer where to put the files.
  1964.                                          If your program has many 
  1965. statements saying to write onto file 3, the computer will write 
  1966. many lines onto file 3, so the file will become long.
  1967.                                          Use carriage controls 
  1968. only if the file is on a line printer or terminal.
  1969.                                          If you say ___ 
  1970.       READ(4,30) X
  1971. the computer will read the value of X from file 4, using line 
  1972. 30's format. If you say file 4 is the card reader, the computer 
  1973. will wait for you to feed in a card; it you say file 4 is your 
  1974. terminal, the computer will wait for you to type on the terminal; 
  1975. if you say file 4 is on a tape or disk, the computer will assume 
  1976. you created the file before running the program.
  1977.                                          For handling files, the 
  1978. word READ works even on PDP computers.
  1979.                                          For PDP-10 and PDP-20 
  1980. computers, here's how to make file 3 be on disk and named JOE. . 
  1981. . . Near the beginning of your program, say:
  1982.       OPEN(UNIT=3, DEVICE='DSK', FILE='JOE.')
  1983. Near the end of your program, say:
  1984.       CLOSE(UNIT=3)
  1985.